<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="Model_Db.DbUtil"%>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@page import="java.awt.Color" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
<jsp:useBean id="e" class="Model_Db.DbUtil" ></jsp:useBean>
	 	<% 
		        	request.setCharacterEncoding("UTF-8");
	 				float num_case=0.0f;
					String sql_clinicaldiagnosis="select ch_clinicaldiagnosis from t_casehistory where ch_patientid='"+request.getParameter("patient_id")+"'";
					ResultSet rs_clinicaldiagnosis=e.QuerySQL(sql_clinicaldiagnosis);
					rs_clinicaldiagnosis.last();
					rs_clinicaldiagnosis.beforeFirst();
		        	List<String> list_clinicaldiagnosis=new ArrayList<String>();//不重复元素
		        	List<String> list_clinicaldiagnosisall=new ArrayList<String>();;//所有元素
		        	List<Float> number=new ArrayList<Float>();//统计各元素的个数
					while(rs_clinicaldiagnosis.next()){
						String clinicaldiagnosis=rs_clinicaldiagnosis.getString("ch_clinicaldiagnosis");
						list_clinicaldiagnosis.add(clinicaldiagnosis);
						list_clinicaldiagnosisall.add(clinicaldiagnosis);
					}
					HashSet hSet = new HashSet(list_clinicaldiagnosis);
					list_clinicaldiagnosis.clear();
					list_clinicaldiagnosis.addAll(hSet);
					float size=list_clinicaldiagnosisall.size();
					for(int i=0;i<list_clinicaldiagnosis.size();i++){
						num_case = Collections.frequency(list_clinicaldiagnosisall,list_clinicaldiagnosis.get(i));
						/* for(int j=0;i<list_clinicaldiagnosisall.size();j++){
							if(String.valueOf(list_clinicaldiagnosisall.get(j)).equals(String.valueOf(list_clinicaldiagnosis.get(i)))){
								num_case+=1;
							}
							num_case=num_case/list_clinicaldiagnosisall.size();
							number.add(num_case); 
						}*/
						num_case=num_case/size;
						number.add(num_case); 
					}
					List<String> list_color=new ArrayList<String>();
					 //红色
			        String red;
			        //绿色
			        String green;
			        //蓝色
			        String blue;
			        //生成随机对象
			        Random random = new Random();
			        //生成红色颜色代码
			        
			        for (int i=0;i<list_clinicaldiagnosis.size();i++){
			        	red = Integer.toHexString(random.nextInt(256)).toUpperCase();
				        //生成绿色颜色代码
				        green = Integer.toHexString(random.nextInt(256)).toUpperCase();
				        //生成蓝色颜色代码
				        blue = Integer.toHexString(random.nextInt(256)).toUpperCase();
				        //判断红色代码的位数
				        red = red.length()==1 ? "0" + red : red ;
				        //判断绿色代码的位数
				        green = green.length()==1 ? "0" + green : green ;
				        //判断蓝色代码的位数
				        blue = blue.length()==1 ? "0" + blue : blue ;
				        //生成十六进制颜色值
			        	String color = "#"+red+green+blue;
			        	list_color.add(color);
			        }
					%>
					
	<script>  
            //绘制饼图  
            function drawCircle(canvasId, data_arr, color_arr, text_arr)  
            {  
                var c = document.getElementById(canvasId);  
                var ctx = c.getContext("2d");  
  
                var radius = c.height / 2 - 20; //半径  
                var ox = radius + 20, oy = radius + 20; //圆心  
  
                var width = 30, height = 10; //图例宽和高  
                var posX = ox * 2 + 20, posY = 30;   //  
                var textX = posX + width + 5, textY = posY + 10;  
  
                var startAngle = 0; //起始弧度  
                var endAngle = 0;   //结束弧度  
                for (var i = 0; i < data_arr.length; i++)  
                {  
                    //绘制饼图  
                    endAngle = endAngle + data_arr[i] * Math.PI * 2; //结束弧度  
                    ctx.fillStyle = color_arr[i];  
                    ctx.beginPath();  
                    ctx.moveTo(ox, oy); //移动到到圆心  
                    ctx.arc(ox, oy, radius, startAngle, endAngle, false);  
                    ctx.closePath();  
                    ctx.fill();  
                    startAngle = endAngle; //设置起始弧度  
  
                    //绘制比例图及文字  
                    ctx.fillStyle = color_arr[i];  
                    ctx.fillRect(posX, posY + 20 * i, width, height);  
                    ctx.moveTo(posX, posY + 20 * i);  
                    ctx.font = 'bold 12px 微软雅黑';    //斜体 30像素 微软雅黑字体  
                    ctx.fillStyle = color_arr[i]; //"#000000";  
                    var percent = text_arr[i] + "：" + 100 * data_arr[i] + "%";  
                    ctx.fillText(percent, textX, textY + 20 * i);  
                }  
            }  
  
            function init() {  
                //绘制饼图  
                //比例数据和颜色  
                var text_arr = new Array();
                var data_arr = new Array();  
                var color_arr = new Array();

                <%
				   if(list_clinicaldiagnosis!=null)
				   {
				    for(int i=0;i<list_clinicaldiagnosis.size();i++)
				    	
				    {
				  %>
				  var bgColor="#"+Math.round(Math.random()*0x1000000).toString(16);
				  
				  text_arr[<%=i%>]='<%=list_clinicaldiagnosis.get(i)%>';
				  data_arr[<%=i%>]='<%=number.get(i)%>';
				  color_arr[<%=i%>]='<%=list_color.get(i)%>';
				  <%   } }
				   %>

                drawCircle("canvas_circle", data_arr, color_arr, text_arr);  
            }  
  
            //页面加载时执行init()函数  
            window.onload = init;  
        </script>
	    <p>  
            <canvas id="canvas_circle" width="500" height="300" style="border:2px solid #0026ff;" >  
                                          浏览器不支持canvas  
            </canvas>
            <br>
            <b>以往病例中该：</b><i>流行性感冒较多，请注意休息，多锻炼提示免疫力！！</i>
        </p>  
</body>
</html>